#include <linux/module.h>
#include <linux/pid.h>
#include <linux/sched.h>
#include <linux/completion.h>
#include <linux/kthread.h>
#include <linux/wait.h>

struct task_struct *pthread;

struct completion cp;

int myfunc_threadtest(void* argc)
{
    printk("进入内核线程函数myfunc_threadtest\n");

    printk("打印输出当前进程的pid值为：%d\n", current->pid);
    printk("当前字段done的值为：%d\n", cp.done);
    printk("父进程的状态:%ld\n", pthread->state);
    

    printk("退出内核线程函数myfunc_threadtest\n");
    return 0;
}

static int __init completionall_init(void)
{
    struct task_struct *pts;
    wait_queue_entry_t data;
    long left_time;

    printk("调用模块初始化函数开始.completionall_init")

    pthread = current;
    pts = kthread_create_on_node(myfunc_threadtest, NULL, -1, "cmpall");
    wake_up_process(pts); // 唤醒新创建的线程


    init_completion(&cp);

    // 使用当前进程初始化等待队列的元素
    init_wait_entry(&data, current);
    add_wait_queue(&(cp.wait), &data);

    // 使等待队列进程不可中断的等待状态
    left_time = schedule_timeout_uninterruptible(10000);

    printk("调用schedule_timeout_uninterruptible函数的timeout的值为：%ld", left_time);
    printk("当前进程的pid的值为：%d\n", current->pid);

    printk("新线程pid的值为：%d\n", pts->pid);

    printk("调用模块初始化函数结束.completionall_init");
    return 0;
}

static void __exit completionall_exit(void)
{
    printk("内核正常退出：completionall(...).\n");
}

MODULE_LICENSE("GPL");

module_init(completionall_init);
module_exit(completionall_exit);